﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using System.Activities.Presentation.Model;
using System.Activities;
using Microsoft.VisualBasic.Activities;

namespace TransactionServer.ActivityDesign.Helpers
{
    class ObjectVariableConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            ModelItem modelItem = value as ModelItem;
            if (value != null)
            {

                if (modelItem.GetCurrentValue() is OutArgument<Object>)
                {
                    OutArgument<Object> outArgument = modelItem.GetCurrentValue() as OutArgument<Object>;

                    if (outArgument != null)
                    {
                        Activity<Location<Object>> expression = outArgument.Expression;
                        VisualBasicReference<Object> vbexpression = expression as VisualBasicReference<Object>;

                        if (vbexpression != null)
                        {
                            return vbexpression.ExpressionText;
                        }
                    }
                }
                else if (modelItem.GetCurrentValue() is InArgument<Object>)
                {
                    InArgument<Object> outArgument = modelItem.GetCurrentValue() as InArgument<Object>;

                    if (outArgument != null)
                    {
                        Activity<Object> expression = outArgument.Expression;
                        VisualBasicValue<Object> vbexpression = expression as VisualBasicValue<Object>;

                        if (vbexpression != null)
                        {
                            return vbexpression.ExpressionText;
                        }
                    }
                }

            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            if (targetType == typeof(OutArgument<Object>))
            {
                VisualBasicReference<Object> vbArgument = new VisualBasicReference<Object>(value.ToString());
                return new OutArgument<Object>(vbArgument);
            }
            else if (targetType == typeof(InArgument<Object>))
            {
                VisualBasicValue<Object> vbArgument = new VisualBasicValue<Object>(value.ToString());
                return  InArgument<Object>.FromExpression(vbArgument);
            }

            return null;
        }
    }
}
